<?php

/**
 * Description of user
 *
 * @author trambauern
 */
class user {

    public function login($username, $password) {
        global $connection;
        $sql = 'SELECT * FROM ' . DB_LOGIN . '.account_data WHERE name = :username AND password = :password AND access_level >= 1';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':username', $username);
        $password = base64_encode(sha1($password, true));
        $dbh->bindValue(':password', $password);
        $dbh->execute();
        $num = $dbh->rowCount();
        if ($num === 1) {
            $row = $dbh->fetch(PDO::FETCH_ASSOC);
            $_SESSION = $row;
            return 1;
        } else {
            return 0;
        }
    }

    /*
     * Get all Accounts
     */

    public function allAccounts($status = '') {
        global $connection;
		if(!is_numeric($status)) {
			$sql = 'SELECT id FROM ' . DB_LOGIN . '.account_data';
			$dbh = $connection->prepare($sql);
			$dbh->execute();
			$num = $dbh->rowCount();
		} elseif($status == '0' OR $status == '1') {
			$sql = 'SELECT id FROM '.DB_LOGIN.'.account_data WHERE activated = :activated';
			$dbh = $connection->prepare($sql);
			$dbh->bindValue(':activated', $status);
			$dbh->execute();
			$num = $dbh->rowCount();
		}
        return $num;
    }

    public function stars($data) {
        if ($data === '1') {
            $star = '<img src="_images/icons/star.png" />';
        } elseif ($data === '2') {
            $star = '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
        } elseif ($data === '3') {
            $star = '<img src="_images/icons/star.png" />'; #
            $star .= '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
        } elseif ($data === '4') {
            $star = '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
        } elseif ($data === '5') {
            $star = '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
            $star .= '<img src="_images/icons/star.png" />';
        }
        return $star;
    }

    /*
     * Function get User Name from Account_data
     */

    public function getName($data) {
        global $connection;

        $sql = 'SELECT id, name FROM ' . DB_LOGIN . '.account_data WHERE id = :id';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':id', $data);
        $dbh->execute();
        $row = $dbh->fetch(PDO::FETCH_ASSOC);

        return $row['name'];
    }

    /*
     * Function get User ID from Account_data
     */

    public function getID($data) {
        global $connection;

        $sql = 'SELECT id, name FROM ' . DB_LOGIN . '.account_data WHERE name = :name';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':name', $data);
        $dbh->execute();
        $row = $dbh->fetch(PDO::FETCH_ASSOC);
        return $row['id'];
    }

    /*
     * Search Email by Name or ID 
     */

    public function getEmail($data) {
        global $connection;

        $sql = 'SELECT id, name FROM ' . DB_LOGIN . '.account_data WHERE id = :data OR name = :data';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':data', $data);
        $dbh->execute();
        $row = $dbh->fetch(PDO::FETCH_ASSOC);
        return $row['email'];
    }

    /*
     * Search Email by Name or ID 
     */

    public function getPW($data) {
        global $connection;
        $sql = 'SELECT id, name, password FROM ' . DB_LOGIN . '.account_data WHERE id = :data OR name = :data';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':data', $data);
        $dbh->execute();
        $row = $dbh->fetch(PDO::FETCH_ASSOC);
        return $row['password'];
    }

    public function addCoins($userID, $data = '') {
        global $connection;
        $sql = 'UPDATE ' . DB_LOGIN . '.account_data SET credits = credits + :credits WHERE id = :userID';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':credits', $data);
        $dbh->bindValue(':userID', $userID);
        $dbh->execute();
    }

    public function setCoins($userID, $data = '') {
        global $connection;
        $sql = 'UPDATE ' . DB_LOGIN . '.account_data SET credits = :credits WHERE id = :userID';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':credits', $data);
        $dbh->bindValue(':userID', $userID);
        $dbh->execute();
    }

    public function sendIngameMail($title, $charID, $message, $sendername = 'Support', $itemid = '0') {
        global $connection;
        $sql = 'SELECT mailUniqueId FROM ' . DB_GAME . '.mail ORDER BY mailUniqueId DESC';
        $dbh = $connection->prepare($sql);
        $dbh->execute();
        $row = $dbh->fetch(PDO::FETCH_ASSOC);
        if (empty($row['mailUniqueId'])) {
            $row['mailUniqueId'] = 0;
        }
        $mainUnique = $row['mailUniqueId'] + 1;
        $sql = 'INSERT INTO ' . DB_GAME . '.`mail` (
					`mailUniqueId`, 
					`mailRecipientId`, 
					`senderName`, 
					`mailTitle`, 
					`mailMessage`, 
					`unread`, 
					`attachedItemId`, 
					`attachedKinahCount`, 
					`express`, 
					`recievedTime`
					) VALUES (
					:mainUnique,
					:charID, 
					:sendername, 
					:title, 
					:message, 
					"1", 
					:itemID, 
					"0", 
					"1", 
					CURRENT_TIMESTAMP)';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':sendername', $sendername);
        $dbh->bindValue(':mainUnique', $mainUnique);
        $dbh->bindValue(':itemID', $itemid);
        $dbh->bindValue(':charID', $charID);
        $dbh->bindValue(':title', $title);
        $dbh->bindValue(':message', $message);
        $dbh->execute();
    }

}

?>
